package io.zhifei.modules.sys.system.controller;


import com.baidubce.BceClientConfiguration;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.lss.LssClient;
import com.baidubce.services.lss.model.ListStreamRequest;
import com.baidubce.services.lss.model.ListStreamResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.zhifei.common.annotation.SysLog;
import io.zhifei.common.utils.ApiResponse;
import io.zhifei.common.utils.DateUtils;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.modules.app.login.annotation.Login;
import io.zhifei.modules.app.login.annotation.LoginUser;
import io.zhifei.modules.app.login.form.UserFrom;
import io.zhifei.modules.sys.system.entity.SysLive;
import io.zhifei.modules.sys.system.service.SysLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p> 百度直播 </p>
 *
 * @author： WangPeng <br/>
 * @date： 2020/10/23 16:26 <br/>
 * @version： <br/>
 */
@CrossOrigin //跨域
@RestController
@RequestMapping("/sys/baiDuLive")
@Api(value = "baiDuLive", description = "百度直播")
public class BaiDuLiveController extends AbstractController {

    @Value("${baidu.accessKeyId}")
    private String ACCESS_KEY_ID;

    @Value("${baidu.secretAccessKey}")
    private String SECRET_ACCESS_KEY;

    @Value("${baidu.pullAddress}")
    private String pullAddress;

    @Value("${baidu.pushAddress}")
    private String pushAddress;

    @Autowired
    private SysLiveService sysLiveService;

    @SysLog("直播新增")
    @PostMapping("addLive")
    @ApiOperation("创建直播")
    public ApiResponse<Object> addLive(@RequestBody SysLive live) {
        try {
            live.setCreateTime(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
            live.setCreateUser(getUserId());
            return sysLiveService.addLive(live, pushAddress, pullAddress);
        } catch (Exception e) {
            return new ApiResponse<>("500", "新增失败！");
        }
    }

    @SysLog("直播列表")
    @GetMapping("liveList")
    @ApiOperation("直播列表")
    public ApiResponse<Object> liveList(@RequestParam String type,
                                        @RequestParam Integer page,
                                        @RequestParam Integer pageSize) {
        try {
            PageUtils pageUtils = sysLiveService.liveList(type, page, pageSize);
            return new ApiResponse<>(pageUtils);
        } catch (Exception e) {
            return new ApiResponse<>("500", e.getMessage());
        }
    }

    @SysLog("编辑直播")
    @PostMapping("updateLive")
    @ApiOperation("编辑直播")
    public ApiResponse<Object> updateLive(@RequestBody SysLive sysLive) {
        try {
            sysLiveService.updateById(sysLive);
            return new ApiResponse<>("200", "修改成功！");
        } catch (Exception e) {
            return new ApiResponse<>("500", "修改失败！");
        }
    }

    @SysLog("删除直播")
    @PostMapping("deleteLive")
    @ApiOperation("删除直播")
    public ApiResponse<Object> deleteLive(@RequestBody String[] ids) {
        try {
            sysLiveService.deleteLive(ids);
            return new ApiResponse<>("200", "修改成功！");
        } catch (Exception e) {
            return new ApiResponse<>("500", "修改失败！");
        }
    }
//    @Login
//    @GetMapping("createStream")
//    @ApiOperation("创建直播")
//    public ApiResponse<Object> bookList(@LoginUser UserFrom userFrom
//    ) {
//
//        return new ApiResponse<>();
//    }

    @Login
    @GetMapping("bulletChat")
    @ApiOperation("弹幕")
    public ApiResponse<Object> bulletChat(@LoginUser UserFrom userFrom,
                                          String roomNumber) {

        return new ApiResponse<>();
    }

    @Login
    @GetMapping("steamList")
    @ApiOperation("在服务器上查询所有流")
    public ApiResponse<Object> steamList(@LoginUser UserFrom userFrom
    ) {
        BceClientConfiguration config = new BceClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
        LssClient client = new LssClient(config);
        ListStreamRequest request = new ListStreamRequest();
        String status = "READY";
        request.withPlayDomain(pullAddress).withStatus(status); // status表示Stream的状态，有效值有READY、ONGOING、PAUSED
        ListStreamResponse response = client.listStream(request);
        return new ApiResponse<>();
    }
}
